package dk.pun.aot.server;

import java.util.List;
import java.util.logging.Logger;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;

import dk.pun.aot.shared.Preferences;
import dk.pun.aot.shared.character.CharacterData;
import dk.pun.aot.shared.user.UserData;

@SuppressWarnings("serial")
public class AlphaOmegaToolsServiceImpl extends RemoteServiceServlet implements dk.pun.aot.client.AlphaOmegaToolsService {

	private static final Logger LOG = Logger.getLogger(AlphaOmegaToolsServiceImpl.class.getName());
	
	@Override
	public UserData getUserData(String userId) {
		PersistenceManager pm = PMF.getPersistenceManager();
		UserData userData = null;
		if (userId != null) {
			try {
				Query q = pm.newQuery(UserData.class, "userId == otherUserId");
				q.declareParameters("Long otherUserId");
				@SuppressWarnings("unchecked")
				List<UserData> results = (List<UserData>) q.execute(userId);
				if (results.size() > 0) {
					userData = results.get(0);
				} else {
					userData = new UserData(userId);
					pm.makePersistent(userData);
				}
				userData = pm.detachCopy(userData);
			} finally {
				pm.close();
			}
			LOG.fine("Got user data: " + (userData != null ? userData.toString() : userData));
		}
		return userData;
	}

	@Override
	public void updateUserData(UserData userData) {
		if  (userData != null) {
			PersistenceManager pm = PMF.getPersistenceManager();
			try {
				pm.makePersistent(userData);
			} finally {
				pm.close();
			}
			LOG.fine("Updated user data: " + (userData != null ? userData.toString() : userData));
		}
	}

	@Override
	public void deleteUserData(String userId) {
		if (userId != null) {
			PersistenceManager pm = PMF.getPersistenceManager();
			try {
				Query q = pm.newQuery(UserData.class, "userId == otherUserId");
				q.declareParameters("Long otherUserId");
				@SuppressWarnings("unchecked")
				List<UserData> results = (List<UserData>) q.execute(userId);
				if (results.size() > 0) {
					pm.deletePersistent(results.get(0));
				}
			} finally {
				pm.close();
			}
			LOG.fine("Deleted user data: " + userId);
		}
	}

	@Override
	public CharacterData createCharacterData(String name) {
		PersistenceManager pm = PMF.getPersistenceManager();
		CharacterData characterData = null;
		if (name != null) {
			characterData = new CharacterData();
			characterData.setName(name);
			try {
				pm.makePersistent(characterData);
				characterData = pm.detachCopy(characterData);
			} finally {
				pm.close();
			}
		}
		return characterData;
	}

	@Override
	public CharacterData getCharacterData(Long characterKey) {
		PersistenceManager pm = PMF.getPersistenceManager();
		CharacterData characterData = null;
		try {
			if (characterKey == null) {
				characterData = new CharacterData();
				pm.makePersistent(characterData);
				LOG.fine("Created new character data:" + (characterData != null ? characterData.toString() : characterData));
			} else {
				characterData = pm.getObjectById(CharacterData.class, characterKey);
				LOG.fine("Got character data: " + (characterData != null ? characterData.toString() : characterData));
			}
			characterData = pm.detachCopy(characterData);
		} finally {
			pm.close();
		}
		return characterData;
	}

	@Override
	public void updateCharacterData(CharacterData characterData) {
		if (characterData != null) {
			PersistenceManager pm = PMF.getPersistenceManager();
			try {
				CharacterData fetchedCharacterData = pm.getObjectById(CharacterData.class, characterData.getKey());
				characterData.copyContent(fetchedCharacterData);
			} finally {
				pm.close();
			}
			LOG.fine("Updated character data: " + (characterData != null ? characterData.toString() : characterData));
		}
	}
	
	@Override
	public void deleteCharacterData(Long characterKey) {
		if (characterKey != null) {
			PersistenceManager pm = PMF.getPersistenceManager();
			try {
				CharacterData fetchedCharacterData = pm.getObjectById(CharacterData.class, characterKey);
				if (fetchedCharacterData != null) {
					pm.deletePersistent(fetchedCharacterData);
				}
			} finally {
				pm.close();
			}
			LOG.fine("Deleted character data: " + characterKey);
		}
	}

	@Override
	public String getMessage() {
		PersistenceManager pm = PMF.getPersistenceManager();
		String message = "";
		try {
			Query q = pm.newQuery(Preferences.class);
			@SuppressWarnings("unchecked")
			List<Preferences> results = (List<Preferences>) q.execute();
			if (results.size() > 0) {
				Preferences preferences = results.get(0);
				message = preferences.getMessage();
			}
		} finally {
			pm.close();
		}
		return message;
	}

	@Override
	public void setMessage(String message) {
		if (message != null) {
			PersistenceManager pm = PMF.getPersistenceManager();
			try {
				Query q = pm.newQuery(Preferences.class);
				@SuppressWarnings("unchecked")
				List<Preferences> results = (List<Preferences>) q.execute();
				if (results.size() > 0) {
					Preferences preferences = results.get(0);
					preferences.setMessage(message);
				} else {
					Preferences preferences = new Preferences();
					preferences.setMessage(message);
					pm.makePersistent(preferences);
				}
			} finally {
				pm.close();
			}
		}
	}

}
